<template>
  <van-button
    class="follow-btn"
    round
    size="small"
    v-if="isFollowed"
    :loading="loading"
    @click="onFollow"
    >已关注</van-button
  >

  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading="loading"
    @click="onFollow"
    >关注</van-button
  >
</template>
<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: 'FollowUser',
  components: {},
  model: {
    prop: 'isFollowed', //默认是Value
    event: 'update-is_followed', //默认是input
  },
  props: {
    isFollowed: {
      type: Boolean,
      required: true,
    },
    userId: {
      type: [Number, String, Object],
      required: true,
    },
  },
  data() {
    return {
      loading: false,
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onFollow() {
      this.loading = true
      try {
        if (this.isFollowed) {
          //   已关注，取消关注
          const { data } = await deleteFollow(this.userId)
          console.log(data)
          //   this.article.is_followed = false
        } else {
          //   没有关注，点击关注
          const { data } = await addFollow(this.userId)
          console.log(data)
          //   this.article.is_followed = true
        }
        // this.article.is_followed = !this.article.is_followed
        this.$emit('update-is_followed', !this.isFollowed)
      } catch (err) {
        console.log(err)
        let message = '操作失败，请稍后重试'
        if (err.response && err.response.status === 400) {
          message = '你自己不能关注自己'
        }
        this.$toast(message)
      }
      this.loading = false
    },
  },
}
</script>
<style lang="less" scoped></style>
